package ink.xiaobaibai.common;

import ink.xiaobaibai.emqtt.EMqttManager;
import ink.xiaobaibai.emqtt.EMqttService;
import ink.xiaobaibai.emqtt.MyMqttCallback;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttMessage;

/**
 * @description: 安全辅助类
 * @author: 小白白
 * @create: 2021-05-19
 **/

public class MySecurity {

    /**
     * 当前请求对应的用户id
     */
    public static ThreadLocal<Integer> threadLocalId = new ThreadLocal<>();

    public static Integer getUserId() {
        return threadLocalId.get();
    }

    public static void main(String[] args) {
        EMqttService mqttService1 = EMqttManager.getClientTest("oz_1", "oz_1", "ss1", new MyMqttCallback() {
            @Override
            protected void connectionLost2(Throwable throwable) {

            }

            @Override
            protected void messageArrived2(String s, MqttMessage mqttMessage) {
                String content = new String(mqttMessage.getPayload());
                System.out.println("臭氧接收到了消息:" + content);
            }

            @Override
            protected void deliveryComplete2(IMqttDeliveryToken iMqttDeliveryToken) {

            }
        });
        mqttService1.start();
        EMqttService mqttService2 = EMqttManager.getClientTest("ox_115", "ox_115", "ss2", new MyMqttCallback() {
            @Override
            protected void connectionLost2(Throwable throwable) {

            }

            @Override
            protected void messageArrived2(String s, MqttMessage mqttMessage) {
                String content = new String(mqttMessage.getPayload());
                System.out.println("氧气接收到了消息:" + content);
            }

            @Override
            protected void deliveryComplete2(IMqttDeliveryToken iMqttDeliveryToken) {

            }
        });
        mqttService2.start();
    }

}
